<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/xxxbin/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/xxxbin/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/xxxbin/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="javascript," />








  <link rel="shortcut icon" type="image/x-icon" href="/xxxbin/favicon.ico?v=5.1.0" />






<meta name="description" content="let 命令 基本用法&amp;emsp;&amp;emsp;说到 let，没学过 es6 语法的同学应该会懵x，但是说到 var 同学们再熟悉不过了，var 就是一个声明变量的关键字，但 let 也是用来声明变量的关键字，但是 let 命令声明的变量只能在代码块里有效，如下： 123456&amp;#123;  let a = 1;  var b = 2;&amp;#125;console.log(a);  // a is">
<meta name="keywords" content="javascript">
<meta property="og:type" content="article">
<meta property="og:title" content="我的es6学习路之let和const命令">
<meta property="og:url" content="https://xxxbin.github.io/2017/04/13/1-我的es6学习路之let和const命令/index.html">
<meta property="og:site_name" content="斌临城下1996">
<meta property="og:description" content="let 命令 基本用法&amp;emsp;&amp;emsp;说到 let，没学过 es6 语法的同学应该会懵x，但是说到 var 同学们再熟悉不过了，var 就是一个声明变量的关键字，但 let 也是用来声明变量的关键字，但是 let 命令声明的变量只能在代码块里有效，如下： 123456&amp;#123;  let a = 1;  var b = 2;&amp;#125;console.log(a);  // a is">
<meta property="og:updated_time" content="2017-04-13T13:22:09.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="我的es6学习路之let和const命令">
<meta name="twitter:description" content="let 命令 基本用法&amp;emsp;&amp;emsp;说到 let，没学过 es6 语法的同学应该会懵x，但是说到 var 同学们再熟悉不过了，var 就是一个声明变量的关键字，但 let 也是用来声明变量的关键字，但是 let 命令声明的变量只能在代码块里有效，如下： 123456&amp;#123;  let a = 1;  var b = 2;&amp;#125;console.log(a);  // a is">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/xxxbin/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://xxxbin.github.io/2017/04/13/1-我的es6学习路之let和const命令/"/>





  <title> 我的es6学习路之let和const命令 | 斌临城下1996 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  














  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/xxxbin/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">斌临城下1996</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/xxxbin/./" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/xxxbin/./archives/index.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/xxxbin/./tags/index.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/xxxbin/./about/index.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://xxxbin.github.io/xxxbin/2017/04/13/1-我的es6学习路之let和const命令/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Bin">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/xxxbin/./uploads/1.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="斌临城下1996">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                我的es6学习路之let和const命令
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-04-13T14:54:45+08:00">
                2017-04-13
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<h1 id="let-命令"><a href="#let-命令" class="headerlink" title="let 命令"></a>let 命令</h1></blockquote>
<h2 id="基本用法"><a href="#基本用法" class="headerlink" title="基本用法"></a>基本用法</h2><p>&emsp;&emsp;说到 let，没学过 es6 语法的同学应该会懵x，但是说到 var 同学们再熟悉不过了，var 就是一个声明变量的关键字，但 let 也是用来声明变量的关键字，但是 let 命令声明的变量只能在代码块里有效，如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">&#123;</div><div class="line">  let a = 1;</div><div class="line">  var b = 2;</div><div class="line">&#125;</div><div class="line">console.log(a);  // a is not defined (a没有定义)</div><div class="line">console.log(b);  // 2</div></pre></td></tr></table></figure>
<p>上面的代码已经表明了 let 只能在代码块内才能访问， let 对 for 循环非常友好，如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">for(var i = 0;i &lt; 10;i++)&#123;&#125;</div><div class="line">console.log(i);  // 10</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">for(let i = 0;i &lt; 10;i++)&#123;&#125;</div><div class="line">console.log(i);  // i is not defined (i没有定义)</div></pre></td></tr></table></figure>
<p>这很好的证明了，let 只能在代码块内才能访问，代码块外不能访问。</p>
<h2 id="let-不存在变量提升"><a href="#let-不存在变量提升" class="headerlink" title="let 不存在变量提升"></a>let 不存在变量提升</h2><p>let 不像 var 那样存在变量提升，let 必须先声明变量后才能使用该变量</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">console.log(a); // Uncaught ReferenceError: a is not defined (发生一个引用错误， a 未定义)</div><div class="line">let a = 1;</div></pre></td></tr></table></figure>
<h2 id="let-不允许重复声明"><a href="#let-不允许重复声明" class="headerlink" title="let 不允许重复声明"></a>let 不允许重复声明</h2><p>let 不允许在相同的作用域下重复声明同一个变量。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">function fn() &#123;</div><div class="line">  var a = 1;</div><div class="line">  let a = 2;</div><div class="line">&#125;</div><div class="line"></div><div class="line">fn(); // Identifier &apos;a&apos; has already been declared (标识符“a”已被声明)</div></pre></td></tr></table></figure>
<p>也不能重新声明变量。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">function fn(a) &#123;</div><div class="line">  let a = 1;</div><div class="line">&#125;</div><div class="line"></div><div class="line">fn(); // Identifier &apos;a&apos; has already been declared (标识符“a”已被声明)</div></pre></td></tr></table></figure>
<p>这样可以：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">function fn(a) &#123;</div><div class="line">  &#123;</div><div class="line">    let a = 1;  这样不报错</div><div class="line">  &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">fn();</div></pre></td></tr></table></figure>
<p>但是这样不是我们想要的结果。</p>
<h1 id="const-命令"><a href="#const-命令" class="headerlink" title="const 命令"></a>const 命令</h1><p>const 是用来声明常量的。而常量一旦被声明就不能被修改。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">const a = 1;</div><div class="line">console.log(a); a // 1</div><div class="line"></div><div class="line">a = 2; // Uncaught TypeError: Assignment to constant variable.</div></pre></td></tr></table></figure>
<p>所以就意味着使用 const 来定义常量，就必须立即赋值，不能留到后面再赋值。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">const a;  // Missing initializer in const declaration (const声明中缺少初始化)</div></pre></td></tr></table></figure>
<p>const 跟 let 一样也是必须先声明后才能使用。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">console.log(a); // Uncaught ReferenceError: a is not defined (发生一个引用错误，a 未定义)</div><div class="line">const a = 1;</div></pre></td></tr></table></figure>
<p>const 跟 let 一样也是不能重复定义变量。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">var a = 1;</div><div class="line">let b = 2;</div><div class="line">const a = 11; // Identifier &apos;a&apos; has already been declared (标识符“a”已被声明)</div><div class="line">const b = 22; // Identifier &apos;b&apos; has already been declared (标识符“b”已被声明)</div></pre></td></tr></table></figure>
<blockquote><footer><strong>本文参考 阮一峰</strong><cite>es6标准入门</cite></footer></blockquote>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/xxxbin/tags/javascript/" rel="tag"># javascript</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/xxxbin/2017/04/13/第一次玩博客/" rel="next" title="第一次玩博客">
                <i class="fa fa-chevron-left"></i> 第一次玩博客
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/xxxbin/./uploads/1.jpg"
               alt="Bin" />
          <p class="site-author-name" itemprop="name">Bin</p>
           
              <p class="site-description motion-element" itemprop="description">吹水小博客</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/xxxbin/./archives/index.html">
                <span class="site-state-item-count">2</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/xxxbin/tags/index.html">
                <span class="site-state-item-count">1</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/xxxbin" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#let-命令"><span class="nav-number">1.</span> <span class="nav-text">let 命令</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#基本用法"><span class="nav-number">1.1.</span> <span class="nav-text">基本用法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#let-不存在变量提升"><span class="nav-number">1.2.</span> <span class="nav-text">let 不存在变量提升</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#let-不允许重复声明"><span class="nav-number">1.3.</span> <span class="nav-text">let 不允许重复声明</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#const-命令"><span class="nav-number">2.</span> <span class="nav-text">const 命令</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder"><a href="https://github.com/xxxbin">Bin</a></span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="#">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  








  
  <script type="text/javascript" src="/xxxbin/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/xxxbin/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/xxxbin/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/xxxbin/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/xxxbin/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/xxxbin/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/xxxbin/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/xxxbin/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/xxxbin/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/xxxbin/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/xxxbin/js/src/bootstrap.js?v=5.1.0"></script>



  


  




	





  





  





  






  





  

  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
